<?php

/* 购物车 cart */
class CartModel extends BaseModel
{
    var $table  = 'cart';
    var $prikey = 'rec_id';
    var $_name  = 'cart';

    var $_relation = array(
        'belongs_to_store'  => array(
            'type'      =>  BELONGS_TO,
            'model'     =>  'store',
            'reverse'   =>  'has_cart',
        ),
        'belongs_to_goodsspec'  => array(
            'type'      =>  BELONGS_TO,
            'model'     =>  'goodsspec',
            'foreign_key' => 'spec_id',
            'reverse'   =>  'has_cart_items',
        ),
    );

    /**
     *    获取商品种类数
     *
     *    @author    Garbin
     *    @return    void
     */
    function get_kinds($sess_id, $user_id = 0)
    {
        $where_user_id = $user_id ? " AND user_id={$user_id}" : '';
        $kinds = $this->db->getOne("SELECT COUNT(DISTINCT goods_id) as c FROM {$this->table} WHERE session_id='{$sess_id}'{$where_user_id}");

        return $kinds;
    }
    /**
     *    更改购物车按数量报价适合条件的价格
     *
     *    @author    yanghj 2014.7.2
     *    @param     $retail_price   价格零售价
     *    @param     $price   商品实际价格
     *    @param     $where   更新条件
     *    @return    void
     */
    function edit_cart_price($retail_price, $price, $where)
    {
        if(!$retail_price && !$where)
        {
            return false;
        }
        $price = floatval($price) > 0 ? $price : $retail_price;
        $this->edit($where,array('price'=>$price,'retail_price'=>$retail_price));
        if ($this->has_error())
        {
            $this->show_warning($this->get_error());

            return;
        }       

        return true;
    }
    /**
     *    获取商品种类数
     *
     *    @author    yanghj 2014.7.2
     *    @param     $sess_id   购物车SESS_ID
     *    @param     $goods_id  商品ID
     *    @return    integer
     */
    function get_goods_totalnum($sess_id, $goods_id)
    {
        $totalnum = $this->db->getOne("SELECT SUM(quantity) FROM {$this->table} WHERE session_id='{$sess_id}' AND goods_id = {$goods_id}");

        return intval($totalnum);
    }
    
    	/**
     *    按购物车的商品数量取得产品价格
     *    goods_id  产品ID
     *    number    购物车的产品数量
     *    @return    price
     */
	function get_volume_price($goods_id,$goods_num)
	{
		$volume_price = 0;
		//取得商品优惠价格列表
		$volumeprice_model   =& m('volumeprice');
		$price_list   = $volumeprice_model->get_infos($goods_id);

		if (!empty($price_list))
		{
			if(count($price_list) == 1 || $goods_num <= $price_list[0]['volume_number'] )
			{
				$volume_price = $price_list[0]['volume_price'];
				return $volume_price;
			}
			foreach ($price_list as $value)
			{
				if ($goods_num >= $value['volume_number'])
				{
					$volume_price = $value['volume_price'];
				}
			}
		}

		return $volume_price;
	}
    
    /**
     *    获取购物车最新的5条记录
     *
     *    @author    yanghj 2014.7.5
     *    @param     $sess_id   购物车SESS_ID
     *    @param     $goods_id  商品ID
     *    @return    array
     */
    function get_cart_top($sess_id, $user_id = 0, $limit = 5)
    {
        $arr = array();
        $user_id = intval($user_id);
        $conditions = "session_id='{$sess_id}'";
        if($user_id)
        {
          $conditions = "session_id='{$sess_id}' AND user_id = {$user_id}";  
        }
        $data = $this->find(array(         
            'fields'=> 'rec_id, goods_id, goods_name, specification, price, quantity, goods_image',
            'conditions' => $conditions,
            'order' => 'rec_id DESC',
            'limit' => $limit,
        ));
        if($data)
        {
           sort($data);
           $count_num = $this->db->getRow("SELECT COUNT(*) AS total,SUM(quantity) AS total_quantity, SUM(quantity*price) AS total_price FROM {$this->table} WHERE ".$conditions);
          $arr['total'] = $count_num['total'];
          $arr['total_quantity'] = $count_num['total_quantity'];
          $arr['total_price'] = $count_num['total_price'];
          $arr['cart'] = $data;           
        }        
        return $arr;
    }
}

?>